package com.bky.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author shipeilei
 * @Date Created on 2018/5/3
 */
public class CookieUtils {
    //设置cookie
    public static void setCookie(HttpServletResponse response, String name, String value, int maxAge, String domain) {
        if(name != null) {
            Cookie cookie = new Cookie(name, value);
            if(domain != null && !"".equals(domain.trim())) {
                cookie.setDomain(domain);
            }
            cookie.setPath("/");
            cookie.setMaxAge(maxAge);
            response.addCookie(cookie);
        }
    }
    //从cookie数组中获取指定key的cookie
    public static Cookie getCookieFromCookies(Cookie[] cookies, String target) {
        Cookie cookie = null;
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals(target)) {
                cookie = cookies[i];
            }
        }

        return cookie;
    }

    //删除所有cookie
    public static void removeAllCookies(HttpServletRequest req, HttpServletResponse resp) {
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            cookie.setMaxAge(0);
            cookie.setPath("/");
            resp.addCookie(cookie);
        }
    }

    //根据cookie名称移除cookie
    public static void removeCookieByName(String cookie_name, HttpServletRequest request, HttpServletResponse resp) {
        Cookie cookie = null;
        Cookie[] cookies = null;
        // 获取当前域名下的cookies，是一个数组
        cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                cookie = cookies[i];
                if ((cookie.getName()).compareTo(cookie_name) == 0) {
                    cookie.setMaxAge(0);
                    resp.addCookie(cookie);
                }
            }
        }
    }
}
